//
//  SetViewController.m
//  Cat
//
//  Created by lushitong on 2018/12/6.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "SetViewController.h"
#import "QKActionSheet.h"
#import "VPImageCropperViewController.h"
#import "HttpUserManager.h"
#import "ProfileEditNameViewController.h"
#import "FMDBUser.h"
#import "UserInfoManager.h"
#import "FLImageView.h"

typedef NS_ENUM(NSUInteger, ImagePickerCtrlForEidtItem) {
	///编辑头像
	ImagePickerCtrlForHeadEidt = 1,
	///编辑相册
	ImagePickerCtrlForAlbumEdit,
	
};  //用于标记ImagePickerCtrl打开时，编辑的是哪项内容


@interface SetViewController () <QKActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, VPImageCropperDelegate>

@property (nonatomic, strong) UILabel *firstLabel;
@property (nonatomic, strong) FLImageView *rightImageView;
@property (nonatomic, strong) UIView *firstBottom;
@property (nonatomic, strong) UILabel *nickLabel;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIView *senBottom;
@property (nonatomic, strong) UIButton *fixButton;

@property (nonatomic, strong) UIButton *fixIconButton;
@property (nonatomic, strong) UIButton *fixIDButton;
@property (nonatomic, strong) QKActionSheet *albumImageAddActionSheet;      //增加相册照片
@property (nonatomic, assign) ImagePickerCtrlForEidtItem imagePickerForEditItem;    //某项编辑内容时打开UIImagePickerController
@property (nonatomic, strong) UIImage *headSrcImage;    //头像原图片
@property (nonatomic, strong) UIImage *headEditedImage;   //编辑后的头像图片
@property (nonatomic, strong) FMDBUser *user;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	[self addAllSubViews];
	self.view.backgroundColor = [UIColor whiteColor];
	[self.topTitleView setDetailDic:@{Nav_Title:@"个人资料设置"}.mutableCopy];
	[self.view bringSubviewToFront:self.topTitleView];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	FMDBUser *user = [UserInfoManager userWithPhone:UDPhone];
	self.user = user;
	self.numberLabel.text = user.nickName;
	self.rightImageView.url = self.user.avator;
}

- (void)addAllSubViews {
	[self.view addSubviews:@[self.firstLabel,self.rightImageView,self.firstBottom,self.nickLabel,self.numberLabel,self.senBottom,self.fixButton,self.fixIconButton,self.fixIDButton]];
	
	[self.firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(16));
		make.top.equalTo(self.topTitleView.mas_bottom).offset(kQKFitModuleDecimal(15));
	}];
	
	[self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(40), kQKFitModuleDecimal(40)));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(16));
		make.centerY.equalTo(self.firstLabel.mas_centerY);
	}];
	
	[self.firstBottom mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.top.equalTo(self.firstLabel.mas_bottom).offset(kQKFitModuleDecimal(15));
		make.height.equalTo(@(kQKFitModuleDecimal(1)));
	}];
	
	[self.nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.firstLabel.mas_left);
		make.top.equalTo(self.firstBottom).offset(kQKFitModuleDecimal(15));
	}];
	
	[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(self.nickLabel.mas_centerY);
		make.right.equalTo(self.rightImageView.mas_right);
		make.left.mas_greaterThanOrEqualTo(self.nickLabel.mas_right).offset(kQKFitModuleFloor(10.0)).priorityHigh();
	}];
	
	[self.senBottom mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.top.equalTo(self.nickLabel.mas_bottom).offset(kQKFitModuleDecimal(15));
		make.height.equalTo(@(kQKFitModuleDecimal(1)));
	}];
	
	[self.fixButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.view.mas_left).offset(kQKFitModuleDecimal(27));
		make.right.equalTo(self.view.mas_right).offset(-kQKFitModuleDecimal(27));
		make.top.equalTo(self.senBottom.mas_bottom).offset(kQKFitModuleDecimal(79));
		make.height.equalTo(@(40));
	}];
	
	[self.fixIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.top.equalTo(self.topTitleView.mas_bottom);
		make.bottom.equalTo(self.firstBottom.mas_bottom);
	}];
	
	[self.fixIDButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.right.equalTo(self.view);
		make.top.equalTo(self.firstBottom.mas_bottom);
		make.bottom.equalTo(self.senBottom.mas_bottom);
	}];
}

- (void)saveAction {
	[HttpUserManager userUpdateInfo:self.user.avator withNickName:self.user.nickName withSuccuss:^(NSDictionary * _Nonnull dict) {
		[FLProgressHUD showSuccess:@"保存成功"];
		[self.navigationController popViewControllerAnimated:YES];
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		[FLProgressHUD showError:message];
	}];
}

- (void)showProfile {
	[self.albumImageAddActionSheet show];
}

- (void)qkActionSheet:(QKActionSheet *)actionSheet clickedAtIndex:(NSInteger)index {
	
	if (actionSheet == self.albumImageAddActionSheet) {       //增加相册图片
		self.imagePickerForEditItem = ImagePickerCtrlForAlbumEdit;
		if (index == 0) {   //拍照
			[self imagePickerCtrlWithSourceType:UIImagePickerControllerSourceTypeCamera];
		} else if (index == 1) {    //相册选择
			[self imagePickerCtrlWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
		}
		
	}
}

- (void)fixIDbutton {
	ProfileEditNameViewController *vc = [[ProfileEditNameViewController alloc] init];
	vc.name = self.user.nickName;
	[self.navigationController pushViewController:vc animated:YES];
}

- (void)imagePickerCtrlWithSourceType:(UIImagePickerControllerSourceType)sourceType {
	
	if ((sourceType == UIImagePickerControllerSourceTypeCamera) &&
		(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])) {
		return;
	}
	
	UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init];
	imagePickerCtrl.sourceType = sourceType;
	imagePickerCtrl.delegate = self;
	
	imagePickerCtrl.navigationBar.tintColor = [UIColor qkNavTintColor];
	
	[self presentViewController:imagePickerCtrl animated:YES completion:NULL];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
	
	UIImage *image = info[UIImagePickerControllerOriginalImage];
	self.headSrcImage = image;
	
	VPImageCropperViewController *vc = [[VPImageCropperViewController alloc] initWithImage:image cropFrame:CGRectMake(0, (ScreenHeight - ScreenWidth) / 2.0, ScreenWidth, ScreenWidth) limitScaleRatio:[UIScreen mainScreen].scale];
	vc.delegate = self;
	[picker pushViewController:vc animated:NO];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
	[picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - VPImageCropperDelegate

- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
	self.headEditedImage = editedImage;
	self.rightImageView.loacalImage = editedImage;
	[HttpUserManager upLoadFile:editedImage andSuccessInfo:^(NSString * _Nonnull url) {
		self.rightImageView.url = url;
		self.user.avator = url;
	} failure:^(NSInteger code, NSString * _Nonnull message) {
		
	}];
	[cropperViewController.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
	self.headSrcImage = nil;
	self.headEditedImage = nil;
	[cropperViewController.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark----- lazy init -----

- (UILabel *)firstLabel {
	if (!_firstLabel) {
		_firstLabel = [UILabel labelWithText:@"头像" font:[UIFont systemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4A4A4A)];
	}
	return _firstLabel;
}

- (FLImageView *)rightImageView {
	if (!_rightImageView) {
		_rightImageView = [[FLImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, kQKFitModuleDecimal(40), kQKFitModuleDecimal(40))];
		_rightImageView.layer.cornerRadius = kQKFitModuleDecimal(20);
		_rightImageView.layer.masksToBounds = YES;
	}
	return _rightImageView;
}

- (UIView *)firstBottom {
	if (!_firstBottom) {
		_firstBottom = [UIView new];
		_firstBottom.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	}
	return _firstBottom;
}

- (UILabel *)nickLabel {
	if (!_nickLabel) {
		_nickLabel = [UILabel labelWithText:@"昵称" font:[UIFont systemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4A4A4A)];
	}
	return _nickLabel;
}

- (UILabel *)numberLabel {
	if (!_numberLabel) {
		_numberLabel = [UILabel labelWithText:@"13545678889" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
	}
	return _numberLabel;
}

- (UIView *)senBottom {
	if (!_senBottom) {
		_senBottom = [UIView new];
		_senBottom.backgroundColor = COLOR_WITH_HEX(0xF3F3F3);
	}
	return _senBottom;
}

- (UIButton *)fixButton {
	if (!_fixButton) {
		_fixButton = [UIButton buttonWithTitle:@"确认修改" color:[UIColor whiteColor] font:[UIFont systemFontOfSize:kQKFitFont(16)] normalImage:[UIImage imageWithColor:COLOR_WITH_HEX(0xFFC236)] cornerRadius:kQKFitModuleDecimal(5)];
		[_fixButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _fixButton;
}

- (UIButton *)fixIconButton {
	if (!_fixIconButton) {
		_fixIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_fixIconButton addTarget:self action:@selector(showProfile) forControlEvents:UIControlEventTouchUpInside];
	}
	return _fixIconButton;
}

- (UIButton *)fixIDButton {
	if (!_fixIDButton) {
		_fixIDButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_fixIDButton addTarget:self action:@selector(fixIDbutton) forControlEvents:UIControlEventTouchUpInside];
	}
	return _fixIDButton;
}

- (QKActionSheet *)albumImageAddActionSheet {
	if (!_albumImageAddActionSheet) {
		_albumImageAddActionSheet = [[QKActionSheet alloc] initWithButtons:@[@"拍照", @"从手机相册选择"]];
		_albumImageAddActionSheet.delegate = self;
	}
	return _albumImageAddActionSheet;
}

@end
